package flc.ast.activity;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityScanResultBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.BitmapUtil;
import stark.common.basic.utils.RxUtil;

public class ScanResultActivity extends BaseAc<ActivityScanResultBinding> {

    public static int type;
    public static Bitmap firstBitmap, secondBitmap;
    private int MAX_BITMAP_SIZE = 4 * 1024 * 1024;

    @Override
    protected int onCreate() {
        return R.layout.activity_scan_result;
    }

    @Override
    protected void initView() {
        mDataBinding.ivScanResultBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.llSave.setOnClickListener(this);

        firstBitmap = BitmapUtil.scaleWhenThan(firstBitmap, MAX_BITMAP_SIZE, 2000, true);
        if (secondBitmap != null) {
            secondBitmap = BitmapUtil.scaleWhenThan(secondBitmap, MAX_BITMAP_SIZE, 2000, true);
        }
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.llSave) {//保存
            saveImg();
        }
    }

    @Override
    protected void initData() {
//        if (type != 1) {
//            firstBitmap = BitmapUtil.rotate(firstBitmap, 90, 0, 0);
//            if (type == 2) {
//                secondBitmap = BitmapUtil.rotate(secondBitmap, 90, 0, 0);
//            }
//        }
        mDataBinding.ivFirstImg.setImageBitmap(firstBitmap);
        if (type == 1 || type == 2) {
            mDataBinding.ivSecondImg.setImageBitmap(secondBitmap);
            mDataBinding.ivSecondImg.setVisibility(VISIBLE);
        } else {
            mDataBinding.ivSecondImg.setVisibility(GONE);
        }
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(com.stark.common.res.R.string.saving));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlSave);
                        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(com.stark.common.res.R.string.save_success);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

}